function showFile(fileName) {
    // 隐藏所有编辑器
    document.querySelectorAll('.editor-container').forEach(container => {
        container.classList.remove('active');
    });
    // 显示选中的文件编辑器
    document.getElementById(`editor-${fileName}`).classList.add('active');
    // 更新文件列表选中状态
    document.querySelectorAll('.file-item').forEach(item => {
        item.classList.remove('active');
    });
    document.querySelector(`.file-item[data-file="${fileName}"]`).classList.add('active');
}

function saveFile(fileName) {
    const content = document.getElementById(`content-${fileName}`).value;
    fetch(`/save_file/${scriptName}/${fileName}`, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify({ content: content })
    })
    .then(response => response.json())
    .then(data => {
        showStatus(data.message || '保存成功', 'success');
    })
    .catch(error => {
        showStatus(error.message || '保存失败', 'error');
    });
}

function showStatus(message, type) {
    const statusDiv = document.getElementById('statusMessage');
    statusDiv.textContent = message;
    statusDiv.className = `status-message ${type}`;
    statusDiv.style.display = 'block';
    setTimeout(() => {
        statusDiv.style.display = 'none';
    }, 3000);
}

// 显示第一个文件
document.addEventListener('DOMContentLoaded', () => {
    const firstFile = document.querySelector('.file-item');
    if (firstFile) {
        showFile(firstFile.dataset.file);
    }
}); 